#
# Copyright 2020 The XLS Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Extracts delay model from a text proto, constructs C++ lookup code."""

from absl import app
from absl import flags
import jinja2

from google.protobuf import text_format
from xls.common import runfiles
from xls.estimators import estimator_model
from xls.estimators import estimator_model_pb2

flags.DEFINE_string(
    'model_name',
    None,
    'Name of model. Should be a short string (e.g., "asap7"). Used as the '
    'identifier when accessing the mode via xls::GetDelayEstimator.',
)
flags.DEFINE_enum(
    'precedence',
    None,
    help='Precedence of model.',
    enum_values=('kLow', 'kMedium', 'kHigh'),
)
flags.mark_flag_as_required('model_name')
flags.mark_flag_as_required('precedence')
FLAGS = flags.FLAGS


def main(argv):
  if len(argv) > 2:
    raise app.UsageError('Too many command-line arguments.')

  with open(argv[1], 'rb') as f:
    contents = f.read()

  em = estimator_model.EstimatorModel(
      text_format.Parse(contents, estimator_model_pb2.EstimatorModel())
  )

  # Make sure that `dm` is an EstimatorModel estimating delay
  if not em.is_delay_model():
    raise ValueError('generate_delay_lookup only supports delay estimator.')

  env = jinja2.Environment(undefined=jinja2.StrictUndefined)
  tmpl_text = runfiles.get_contents_as_text(
      'xls/estimators/delay_model/generate_delay_lookup.tmpl'
  )
  template = env.from_string(tmpl_text)
  rendered = template.render(
      delay_model=em,
      name=FLAGS.model_name,
      precedence=FLAGS.precedence,
      camel_case_name=''.join(
          s.capitalize() for s in FLAGS.model_name.split('_')
      ),
  )
  print(
      '// DO NOT EDIT: this file is AUTOMATICALLY GENERATED by'
      ' generate_delay_lookup.py from {} and should not be changed.'.format(
          argv[1]
      )
  )
  print(rendered)


if __name__ == '__main__':
  app.run(main)
